#include <iostream>
#include <fstream>
#include <string>
#include <io.h>

#include "wav.h"

#define BUFFER_SAMPLES  44100

#define WAV_CHANNEL     2

#define AUDIO_CHANNEL           WAV_CHANNEL
#define AUDIO_RATE              44100
#define AUDIO_SAMPLEBYTE        4
#define AUDIO_DATABIT           16


typedef short SAMPLE_TYPE;

using namespace std;

SAMPLE_TYPE sampleBuffer0[BUFFER_SAMPLES*WAV_CHANNEL];
SAMPLE_TYPE sampleBuffer1[BUFFER_SAMPLES*WAV_CHANNEL];
SAMPLE_TYPE sampleBuffer2[BUFFER_SAMPLES*WAV_CHANNEL];
SAMPLE_TYPE sampleBuffer3[BUFFER_SAMPLES*WAV_CHANNEL];
SAMPLE_TYPE sampleBufferMake[BUFFER_SAMPLES*WAV_CHANNEL*4];

int main(int argc, char const *argv[])
{
    unsigned int wav0Sample,wav1Sample,wav2Sample,wav3Sample,samplesCountdonw,r;
    ofstream outFile;

    if(argc < 4)
    {
        fprintf(stderr,"缺少参数\r\n");
        return 0;
    }
    string pathOut = argv[1];
    string path0 = argv[2];
    string path1 = argv[3];
    string path2 = argv[4];
    string path3 = argv[5];
    

    fprintf(stdout,"wav0  :%s\r\n",path0.c_str());
    fprintf(stdout,"wav1  :%s\r\n",path1.c_str());
    fprintf(stdout,"wav2  :%s\r\n",path2.c_str());
    fprintf(stdout,"wav3  :%s\r\n",path3.c_str());
    fprintf(stdout,"outBin:%s\r\n",pathOut.c_str());
    wav *wav0 = new wav();
    wav *wav1 = new wav();
    wav *wav2 = new wav();
    wav *wav3 = new wav();
    if(wav0->open(path0) < 0)
    {
        delete wav0;
        return 0;
    }
    if(wav1->open(path1) < 0)
    {
        delete wav0;
        delete wav1;
        return 0;
    }
    if(wav2->open(path2) < 0)
    {
        delete wav0;
        delete wav1;
        delete wav2;
        return 0;
    }
    if(wav3->open(path3) < 0)
    {
        delete wav0;
        delete wav1;
        delete wav2;
        delete wav3;
        return 0;
    }
    unsigned int rate = wav0->getRate();
    if(rate != AUDIO_RATE)
    {
        fprintf(stderr,"rate error:%d\r\n",rate);
        delete wav0;
        delete wav1;
        delete wav2;
        delete wav3;
        return 0;
    }
    unsigned short channel = wav0->getChannel();
    if(channel != AUDIO_CHANNEL)
    {
        fprintf(stderr,"channel error:%d\r\n",channel);
        delete wav0;
        delete wav1;
        delete wav2;
        delete wav3;
        return 0;
    }
    wav0Sample = wav0->getPCMByteSize();
    wav1Sample = wav1->getPCMByteSize();
    wav2Sample = wav2->getPCMByteSize();
    wav3Sample = wav3->getPCMByteSize();

    if((wav0Sample ^ wav1Sample) ^ (wav2Sample ^ wav3Sample))
    {
        fprintf(stderr,"文件采样点数不相等\r\n");
        delete wav0;
        delete wav1;
        delete wav2;
        delete wav3;
        return 0;
    }
    outFile.open(pathOut,ios::binary | ios::out);
    if(!outFile)
    {
        fprintf(stderr,"创建输出文件失败\r\n");
        delete wav0;
        delete wav1;
        delete wav2;
        delete wav3;
        return 0;
    }
    samplesCountdonw = wav0Sample;
    SAMPLE_TYPE sample0;
    SAMPLE_TYPE sample1;
    SAMPLE_TYPE sample2;
    SAMPLE_TYPE sample3;
    while(samplesCountdonw)
    {
        r = wav0->readSamples((char *)sampleBuffer0,BUFFER_SAMPLES);
        r = wav1->readSamples((char *)sampleBuffer1,BUFFER_SAMPLES);
        r = wav2->readSamples((char *)sampleBuffer2,BUFFER_SAMPLES);
        r = wav3->readSamples((char *)sampleBuffer3,BUFFER_SAMPLES);
        samplesCountdonw -= r;
        for(int i=0; i<r*WAV_CHANNEL; i++)
        {
            sample0 = sampleBuffer0[i];
            sample1 = sampleBuffer1[i];
            sample2 = sampleBuffer2[i];
            sample3 = sampleBuffer3[i];
            sampleBufferMake[i*4 + 0] = sample0;
            sampleBufferMake[i*4 + 1] = sample1;
            sampleBufferMake[i*4 + 2] = sample2;
            sampleBufferMake[i*4 + 3] = sample3;
        }
        outFile.write((const char *)sampleBufferMake,r*2 * WAV_CHANNEL * 4);
    }
    outFile.close();
    delete wav0;
    delete wav1;
    delete wav2;
    delete wav3;
    fprintf(stdout,"合成完成\r\n");

    return 0;
}


int stringToUpper(string& str)
{
    size_t l = str.length();
    for(size_t n=0; n<l; n++)
    {
        if((str[n] >= 'a') && (str[n] <= 'z'))
        {
            str[n] -= 32;
        }
    }
    return 0;
}
